package controllers

import (
	"encoding/json"
	"fmt"
	"gitee.com/yctxkj/cmsvueback/models"
	"github.com/astaxie/beego"
)

type UserController struct {
	beego.Controller
}

// @router /menu [get]
func (c *UserController) Menu() {
	token := c.GetString("token")
	resp := models.UserMenuResp{}

	if token == "admin-token" {
		resp.Code = 20000

		resp.Data.AlwaysShow = true
		resp.Data.Hidden = false
		resp.Data.ID = 100
		resp.Data.Component = ""
		resp.Data.Name = "报表统计"
		resp.Data.Path = ""
		resp.Data.URL = ""
	}

	c.Data["json"] = resp
	c.ServeJSON()
}

// @router /logout [post]
func (c *UserController) Logout() {
	resp := models.UserLoginResp{}

	resp.Code = 20000
	resp.Data.Token = "success"

	c.Data["json"] = resp
	c.ServeJSON()
}

// @router /login [post]
func (c *UserController) Login() {
	//{“username":"admin","password":"111111"}
	//{“code":20000,"data":{"token":"admin-token"}}
	req := models.UserLoginReq{}
	resp := models.UserLoginResp{}
	json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	username := req.Username
	password := req.Password

	fmt.Println(req)
	if username == "admin" && password == "111111" {
		resp.Code = 20000
		resp.Data.Token = "admin-token"
		c.Data["json"] = resp
		c.ServeJSON()
		return
	}

	resp.Code = 60204
	resp.Data.Token = ""
	resp.Message = "账号或密码错误"

	c.Data["json"] = resp
	c.ServeJSON()
}

// @router /info [get]
func (c *UserController) Info() {
	token := c.GetString("token")
	resp := models.UserInfoResp{}

	// {"code":20000,"data":{"roles":["admin"],"introduction":"I am a super administrator","avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif","name":"Super Admin"}}
	if token == "admin-token" {
		resp.Code = 20000
		resp.Data.Name = "超级管理员"
		resp.Data.Introduction = "我是超级管理员，我拥有一切的权利。"
		resp.Data.Avatar = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"
		resp.Data.Roles = []string{"admin"}

		c.Data["json"] = resp
		c.ServeJSON()
		return
	}

	resp.Code = 50008
	resp.Message = "登录失败，不能获取用户信息详情。"
	c.Data["json"] = resp
	c.ServeJSON()
}
